package com.leyou.item.controller;

import com.leyou.common.exception.pojo.ExceptionEnum;
import com.leyou.common.pojo.PageResult;
import com.leyou.item.service.BrandService;
import com.ly.plus.entity.Brand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 分页查询品牌列表
     * @param key //搜索关键字
     * @param page //当前页数
     * @param rows //展示条数
     * @param sortBy //排序字段
     * @param desc //是否排序
     * @return
     */
    @RequestMapping("/brand/page")
    public ResponseEntity<PageResult<Brand>>brandPageQuery(
                                                 @RequestParam(value = "key",required = false) String key,
                                                  @RequestParam(value = "page",defaultValue = "1")  Integer page,
                                                   @RequestParam(value = "rows",defaultValue = "5") Integer rows,
                                                   @RequestParam(value = "sortBy",required = false) String  sortBy,
                                                    @RequestParam(value = "desc",required = false) Boolean desc)
    {

        PageResult<Brand> pageResult =brandService.brandPageQuery(key,page,rows,sortBy,desc);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 添加品牌
     * @param brand //品牌信息
     * @param cids //所属分类id
     */
    @PostMapping("/brand")
    public ResponseEntity<Void> addBrand(Brand brand , @RequestParam("cids")List<Long> cids){
        brandService.addBrand(brand,cids);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     *
     * @param id
     * @return根据id查询单条数据
     */
    @GetMapping("/brand/{id}")
    public ResponseEntity<Brand>findBrandById(@PathVariable("id")Long id){
      Brand brand= brandService.findBrandById(id);
      return ResponseEntity.ok(brand);
    }

    /**
     * 根据分类id查询品牌(多对多)
     * @param id
     * @return
     */
    @GetMapping("/brand/of/category")
    public ResponseEntity<List<Brand>>fingBrandByCid(@RequestParam("id")Long id){
      List<Brand>brands=  brandService.fingBrandByCid(id);
      return ResponseEntity.ok(brands);
    }

    /**
     * 根据id集合获取品牌集合
     * @return
     */
    @GetMapping("/brand/list/of/ids")
    public ResponseEntity<List<Brand>>findBrandByIds(@RequestParam("ids")List<Long>ids){
        List<Brand>brands=brandService.findBrandByIds(ids);
        return ResponseEntity.ok(brands);

    }
}
